Game();
function Game() {
	//browser width and height
    currentWidth = null;
    currentHeight = null;
    // the position of the canvas in relation to the screen
    offset = {top: 0, left: 0};
    // canvas and context
    canvas = null;
    ctx = null;
    //game variables
	var player=null;
	var camera=null;
	var room=null;
	var objectList = new Array();
	
	// requestAnimationFrame instead of intervals for better performance
	window.requestAnimFrame = (function(){
	  return  window.requestAnimationFrame       || 
			  window.webkitRequestAnimationFrame || 
			  window.mozRequestAnimationFrame    || 
			  window.oRequestAnimationFrame      || 
			  window.msRequestAnimationFrame     || 
			  function( callback ){
				window.setTimeout(callback, 1000 / 60);
			  };
	})();	
	
	init();	
	
    function init() {	

    	//set canvas
		currentWidth = window.innerWidth;
		currentHeight = window.innerHeight;
        canvas = document.getElementById("GameCanvas");
        canvas.width = currentWidth;
        canvas.height = currentHeight;
        ctx = canvas.getContext('2d');
		
        // find out android & ios so we can hide the address bar in our resize function
        ua = navigator.userAgent.toLowerCase();
        android = ua.indexOf('android') > -1 ? true : false;
        ios = ( ua.indexOf('iphone') > -1 || ua.indexOf('ipad') > -1  ) ? true : false;
				
		// listen for clicks
		window.addEventListener('click', function(e) {
			e.preventDefault();
			Input(e);
		}, false);
		// listen for touches
		window.addEventListener('touchstart', function(e) {
			e.preventDefault();
			Input(e);
		}, false);
		window.addEventListener('touchmove', function(e) {
			// we're not interested in this, but prevent default behaviour so the screen doesn't scroll or zoom
			e.preventDefault();
		}, false);
		window.addEventListener('touchend', function(e) {
			// as above
			e.preventDefault();
		}, false);
		
		// set up map, player and camera
		room = {
			width: 3000,
			height: 3000,
			map: new Map(3000, 3000)
		};
		player = new Player(canvas.width * 0.5, canvas.height * 0.5);
		room.map.generate();
		camera = new Camera(0, 0, canvas.width-canvas.offsetLeft, canvas.height-canvas.offsetTop, room.width-canvas.offsetLeft, room.height-canvas.offsetTop);		
		camera.follow(player, canvas.width/2, canvas.height/2);
		
		spawnObjects();
        // we're ready to resize
        resize();
        //enter the game loop
		loop();
    }
	
	function spawnObjects() {
		for ( var i = 0; i < 2; i++) {
			var tempObject = new Circle();
			objectList.push(tempObject);
		}
	}
    
    // update function
	function update() {	
		for ( var i = 0; i < 2; i++) {
			objectList[i].update();
			player.checkCollision(objectList[i]);
		}
		player.update();
		camera.update();
	}

	// render function
	function render() {
		//clear screen
	   ctx.clearRect(0, 0, currentWidth, currentHeight);
	   room.map.draw(ctx, camera.xView, camera.yView);	
	   for ( var i = 0; i < 2; i++) {
			objectList[i].draw(ctx, camera.xView, camera.yView);
	   }
	   player.draw(ctx, camera.xView, camera.yView);

	}

	// the actual loop
	// requests animation frame,
	// then proceeds to update
	// and render
	function loop() {
		requestAnimFrame(loop );
		update();
		render();
	}
	
    function resize() {

    	//hide the address bar
        if (android || ios) {
            document.body.style.height = (currentHeight + 50) + 'px';
        }

        // set the new canvas style width and height 
        canvas.style.width = currentWidth + 'px';
        canvas.style.height = currentHeight + 'px';
		
        // position of canvas in relation to the screen
        offset.top = canvas.offsetTop;                       
        offset.left = canvas.offsetLeft;
        // we use a timeout here because some mobile browsers don't fire if there is not a short delay
        window.setTimeout(function() {
                window.scrollTo(0,1);
        }, 1);
    }
	
    // handle the input
	function Input (data) {
		this.x = (data.pageX - offset.left);
		this.y = (data.pageY - offset.top);
		player.checkMouse(this.x + camera.xView, this.y + camera.yView);		
	}
}